/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package extras;

import bd.ConexionBD;
import java.awt.Color;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;

/**
 *
 * @author Isaac
 */
public class ItemGrupo extends javax.swing.JPanel {

    JScrollPane scroll;
    public ContenidoItem pnlDerecho;
    String usuario;
    public ItemGrupo(JScrollPane scroll, int idPersona) {
        initComponents();
        this.scroll = scroll;
        pnlDerecho = new ContenidoItem(idPersona,this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnEliminar = new javax.swing.JButton();
        lblNombre = new javax.swing.JLabel();
        lblGrado = new javax.swing.JLabel();
        btnVisualizar = new javax.swing.JButton();

        setBackground(new java.awt.Color(204, 51, 51));
        setMaximumSize(new java.awt.Dimension(260, 120));
        setMinimumSize(new java.awt.Dimension(260, 120));
        setPreferredSize(new java.awt.Dimension(260, 120));

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/circular.png"))); // NOI18N
        btnEliminar.setBorderPainted(false);
        btnEliminar.setContentAreaFilled(false);
        btnEliminar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnEliminarMousePressed(evt);
            }
        });
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        lblNombre.setFont(new java.awt.Font("Stark", 0, 12)); // NOI18N
        lblNombre.setForeground(new java.awt.Color(255, 255, 255));
        lblNombre.setText("Isaac Edmundo Herrera Salcedo");

        lblGrado.setFont(new java.awt.Font("Walkway UltraBold", 1, 14)); // NOI18N
        lblGrado.setForeground(new java.awt.Color(255, 255, 255));
        lblGrado.setText("Primero");

        btnVisualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/arroow.png"))); // NOI18N
        btnVisualizar.setBorderPainted(false);
        btnVisualizar.setContentAreaFilled(false);
        btnVisualizar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnVisualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(220, 220, 220)
                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(lblGrado, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(220, 220, 220)
                .addComponent(btnVisualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, Short.MAX_VALUE)
                .addGap(1, 1, 1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(btnEliminar)
                .addGap(6, 6, 6)
                .addComponent(lblNombre)
                .addGap(6, 6, 6)
                .addComponent(lblGrado)
                .addGap(21, 21, 21)
                .addComponent(btnVisualizar))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnVisualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarActionPerformed
        scroll.setViewportView(pnlDerecho);
    }//GEN-LAST:event_btnVisualizarActionPerformed

    private void btnEliminarMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEliminarMousePressed
        if(evt.getClickCount() == 2){
            usuario = pnlDerecho.lblNick.getText();
            eliminarUsuario(usuario);
            deshabilitarPanel();
        }
    }//GEN-LAST:event_btnEliminarMousePressed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void deshabilitarPanel(){
        btnEliminar.setEnabled(false);
        btnVisualizar.setEnabled(false);
        setBackground(Color.LIGHT_GRAY);
    }
    private void eliminarUsuario(String usuario){
        try {
            ConexionBD.abrirConexion();
            CallableStatement cs= ConexionBD.con.prepareCall("call eliminarCuenta(?)");
            cs.setString(1, usuario);
            cs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ItemGrupo.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConexionBD.cerrarConexion();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnVisualizar;
    private javax.swing.JLabel lblGrado;
    public javax.swing.JLabel lblNombre;
    // End of variables declaration//GEN-END:variables
}
